package com.apes.scm.settle.settle.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.model.Cooperation;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = TaxBill.CONST_TABLE_NAME)
public class TaxBill extends Model {
    public static final String CONST_TABLE_NAME = "acc_tax_bill";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "税票登记单", prefix = "T")
    private String id; //税票登记单号

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    @JoinColumn(name = "settle_id", referencedColumnName = "id")
    private Settle settle;//结算单

    @States(name = "状态", values = {
            @State(id = "sent", name = "登记"),
            @State(id = "approve", name = "审核")})
    @Column(name = "state", length = 10, nullable = false)
    private String state; //状态

    @Comment("源单据")
    @Column(name = "origin", length = 30)
    private String origin;      // 平台供货结算与客户结算会写这个字段

    @Comment("源单据名称")
    @Column(name = "origin_Business_Name", length = 30)
    private String originBusinessName;

    @OneToMany(mappedBy = "taxBill", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<TaxBillItem> taxBillItems = new HashSet(); //税票明细

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    private Dept dept; //部门

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    private PartyRole partner; //业务伙伴

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cooperation_id", referencedColumnName = "id")
    private Cooperation cooperation; //合作方式

    @Column(name = "amt_total", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double amtTotal = 0; //总计

    @Column(name = "rebate_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double rebateAmount = 0; //折扣金额

    @Column(name = "amt_tax", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double amtTax = 0; //税金

    @Column(name = "amt_untaxed", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    private double amtUntaxed = 0; //无税金额

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    private PartyRole approveUID; //审核人

    @Column(name = "approve_date")
    private Date approveDate; //审核日期

    @Column(name = "payment_month")
    @Comment("货款月份")
    private String paymentMonth;

    @OrderBy("taxBillItems.id asc ")
    public Set<TaxBillItem> getTaxBillItems() {
        if (taxBillItems == null) return new HashSet<TaxBillItem>();
        for (TaxBillItem item : taxBillItems) {
            item.setTaxBill(this);
        }
        return taxBillItems;
    }

    @Transient
    private String settleId;

    public String getSettleId() {
        if (this.getSettle() == null) return null;
        return this.getSettle().getId();
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }

    public void computeAmt() {
        amtTotal = 0;
        amtUntaxed = 0;
        amtTax = 0;
        rebateAmount = 0;
        for (TaxBillItem item : getTaxBillItems()) {
            amtTotal += item.getAmount();
            amtUntaxed += item.getAmount() - item.getTaxAmount();
            amtTax += item.getTaxAmount();
            rebateAmount += item.getRebateAmount();
        }
    }

}
